package com.doctor.windflower_doctor.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.UserControlBeen;
import com.doctor.windflower_doctor.adapter.UserControlAdapter;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.parser.impl.JsonHelper;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.LoadingDialog;
import java.util.List;

public class UserControl extends BaseFragment implements Constent {
	private List<UserControlBeen> list;
	private ListView user_control__listView;
	private UserControlAdapter adapter;
	private int total;
	private TextView title;
	private Dialog dialog;
	private View footView;
	private ImageView right_ImageView;
	private SharedPreferencesDB db;
	private RelativeLayout relativeLayout;
	private Button tryAgainButton;
	private LoadingDialog loadingDialog;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		total = 0;
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		tryAgainButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new User_Control_Asynk().execute(URL + ALL_GROUP_URL
						+ db.getDocotorId());
			}
		});
		footView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				View view = LayoutInflater.from(getActivity()).inflate(
						R.layout.dialog_box_view, null);
				final EditText editText = (EditText) view
						.findViewById(R.id.new_editText);
				Button makesure_button = (Button) view
						.findViewById(R.id.makesure_button);
				Button cancel_button = (Button) view
						.findViewById(R.id.cancel_button);
				cancel_button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});

				makesure_button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (editText.getText().toString().trim().equals("")) {
							Toast.makeText(getActivity(), "请填写新建分组名称",
									Toast.LENGTH_SHORT).show();
						} else {
							new User_Control_Asynk_Add(editText.getText()
									.toString().trim()).execute(URL + ADD_URL
									+ db.getDocotorId());
						}

					}
				});
				dialog.setContentView(view);
				dialog.show();
			}

		});

	}

	class User_Control_Asynk extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			loadingDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.clientGet(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				relativeLayout.setVisibility(View.INVISIBLE);
				user_control__listView.setVisibility(View.VISIBLE);
				list = JsonHelper.userControl_Json(result);
				if (list == null) {
					Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT)
							.show();
				} else {
					adapter.listAddAll(list);
					user_control__listView.setAdapter(adapter);
					if (adapter.isShow()) {
						adapter.setShow(true);
					} else {
						adapter.setShow(false);
					}
					for (int i = 0; i < list.size(); i++) {
						total += Integer
								.parseInt(list.get(i).getNumberOfUser());
					}
					title.setText("用户管理" + "(" + total + ")");
				}

			} else {
				relativeLayout.setVisibility(View.VISIBLE);
				user_control__listView.setVisibility(View.INVISIBLE);
			}
			loadingDialog.close();
		}

	}

	class User_Control_Asynk_Replace extends AsyncTask<String, Void, String> {
		private String groupName;
		private int position;
		private String id;

		private User_Control_Asynk_Replace(String groupName, int position,
				String id) {
			this.position = position;
			this.groupName = groupName;
			this.id = id;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.postReplaceGroupName(groupName, id,
					params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				if (JsonHelper.userControl_Delete_Json(result).equals("true")) {
					adapter.listReplace(groupName, position);
				} else {
					Toast.makeText(getActivity(), "分组名称不能重复命名",
							Toast.LENGTH_SHORT).show();
				}

			} else {
				Toast.makeText(getActivity(), "请连接网络...", Toast.LENGTH_SHORT)
						.show();
			}
			dialog.cancel();
		}

	}

	class User_Control_Asynk_Delete extends AsyncTask<String, Void, String> {
		private String id;
		private int position;

		public User_Control_Asynk_Delete(String id, int position) {
			this.id = id;
			this.position = position;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.postRemoveGroupName(id, params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				if (JsonHelper.userControl_Delete_Json(result).equals("true")) {
					adapter.listRemove(position);
					new User_Control_Asynk().execute(URL + ALL_GROUP_URL
							+ db.getDocotorId());
					dialog.dismiss();
				} else {
					Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT)
							.show();
				}
			} else {
				Toast.makeText(getActivity(), "请连接网络...", Toast.LENGTH_SHORT)
						.show();
			}

		}

	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		return inflater.inflate(R.layout.user_control, container, false);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		new User_Control_Asynk().execute(URL + ALL_GROUP_URL
				+ db.getDocotorId());
		adapter.setShow(false);
		right_ImageView.setImageResource(R.drawable.edit_button);

	}

	@Override
	protected void findViews(View view) {
		// TODO Auto-generated method stub
		db = SharedPreferencesDB.getInstance(getActivity());
		loadingDialog = new LoadingDialog(getActivity());
		tryAgainButton = (Button) view.findViewById(R.id.try_again);
		relativeLayout = (RelativeLayout) view.findViewById(R.id.nowifi_layout);
		footView = LayoutInflater.from(getActivity()).inflate(
				R.layout.footview, null);
		user_control__listView = (ListView) view
				.findViewById(R.id.user_control__listView);
		title = (TextView) ((Activity) getActivity())
				.findViewById(R.id.text_title);
		dialog = new Dialog(getActivity());
		dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		right_ImageView = (ImageView) getActivity()
				.findViewById(R.id.right_img);
		right_ImageView.setImageResource(R.drawable.edit_button);
		user_control__listView.addFooterView(footView);
		adapter = new UserControlAdapter(getActivity(), right_ImageView);
	}

	public void post() {
		// TODO Auto-generated method stub
		if (adapter == null) {
			right_ImageView.setImageResource(R.drawable.edit_button);
			Toast.makeText(getActivity(), "暂无网络连接", Toast.LENGTH_SHORT).show();
		} else if (adapter.getCount() == 1) {
			right_ImageView.setImageResource(R.drawable.edit_button);

		} else {
			if (adapter.isShow()) {
				adapter.setShow(false);
				right_ImageView.setImageResource(R.drawable.edit_button);
			} else {
				adapter.setShow(true);
				right_ImageView.setImageResource(R.drawable.finish);
			}

		}

	}

	class User_Control_Asynk_Add extends AsyncTask<String, Void, String> {
		private String groupName;

		public User_Control_Asynk_Add(String groupName) {
			this.groupName = groupName;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.postAddGroupName(groupName, params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				UserControlBeen controlBeen = JsonHelper
						.userControl_Add_Json(result);
				if (controlBeen != null) {
					SystemApplication.getInstance().isTypeChange = true;
					adapter.listAdd(controlBeen);
				} else {
					Toast.makeText(getActivity(), "用户分组不能重复命名",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(getActivity(), "请检查网络连接", Toast.LENGTH_SHORT)
						.show();
			}

			dialog.cancel();

		}

	}

}
